package cn.online.taxi.sso.service.impl;

import cn.online.taxi.common.constant.RedisKeyConstant;
import cn.online.taxi.common.constant.RedisKeyPrefixConstant;
import cn.online.taxi.common.jedis.JedisClient;
import cn.online.taxi.manager.dto.DtoUtil;
import cn.online.taxi.manager.dto.front.Driver;
import cn.online.taxi.manager.dto.front.DriverReceiving;
import cn.online.taxi.manager.dto.front.Passenger;
import cn.online.taxi.manager.mapper.TbDriverMapper;
import cn.online.taxi.manager.mapper.TbPassengerMapper;
import cn.online.taxi.manager.pojo.TbDriver;
import cn.online.taxi.manager.pojo.TbDriverExample;
import cn.online.taxi.manager.pojo.TbPassenger;
import cn.online.taxi.manager.pojo.TbPassengerExample;
import cn.online.taxi.sso.service.LoginService;
import com.alibaba.dubbo.config.annotation.Service;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author BinBini
 * @Date 2021/3/18 16:28
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private TbPassengerMapper tbPassengerMapper;
    @Resource
    private TbDriverMapper tbDriverMapper;
    @Autowired
    private JedisClient jedisClient;
    @Value("10000")
    private Integer SESSION_EXPIRE;

    @Override
    public Passenger passengerLogin(String phone, String password) {
        TbPassengerExample example = new TbPassengerExample();
        TbPassengerExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(1);
        criteria.andPhoneEqualTo(phone);
        List<TbPassenger> list = tbPassengerMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            Passenger passenger = new Passenger();
            passenger.setState(0);
            passenger.setMessage("账户或密码错误");
            return passenger;
        }
        TbPassenger tbPassenger = list.get(0);
        // md5加密
        if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(tbPassenger.getPassword())) {
            Passenger passenger = new Passenger();
            passenger.setState(0);
            passenger.setMessage("账户或密码错误");
            return passenger;
        }
        String token = UUID.randomUUID().toString();
        Passenger passenger = DtoUtil.TbPassengerToPassenger(tbPassenger);
        passenger.setState(1);
        passenger.setToken(token);
        // 乘客信息写入redis, key: "PASSENGER_LOGIN:token" value: "passenger"
        jedisClient.set(RedisKeyPrefixConstant.PASSENGER_LOGIN_KEY_PRE+ ":" + token, new Gson().toJson(passenger));
        jedisClient.expire(RedisKeyPrefixConstant.PASSENGER_LOGIN_KEY_PRE+ ":" + token, SESSION_EXPIRE);
        return passenger;
    }

    @Override
    public Passenger getPassengerToken(String token) {
        String json = jedisClient.get("SESSION:" + token);
        if (json == null) {
            Passenger passenger = new Passenger();
            passenger.setState(0);
            passenger.setMessage("用户登录已过期");
            return passenger;
        }
        // 重置过期时间
        jedisClient.expire(RedisKeyPrefixConstant.PASSENGER_LOGIN_KEY_PRE+ ":" + token, SESSION_EXPIRE);
        Passenger passenger = new Gson().fromJson(json, Passenger.class);
        return passenger;
    }

    @Override
    public Driver driverLogin(String phone, String password, String driverLongitude, String driverLatitude) {
        TbDriverExample tbDriverExample = new TbDriverExample();
        TbDriverExample.Criteria criteria = tbDriverExample.createCriteria();
        criteria.andUseStatusEqualTo(1);
        criteria.andPhoneEqualTo(phone);
        List<TbDriver> list = tbDriverMapper.selectByExample(tbDriverExample);
        if (list == null || list.size() == 0) {
            Driver driver = new Driver();
            driver.setUseStatus(0);
            driver.setMessage("用户账号或密码错误");
            return driver;
        }
        TbDriver tbDriver = list.get(0);
        if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(tbDriver.getPassword())) {
            Driver driver = new Driver();
            driver.setUseStatus(0);
            driver.setMessage("用户账号或密码错误");
            return driver;
        }
        String token = UUID.randomUUID().toString();
        Driver driver = DtoUtil.TbDriverToDriver(tbDriver);
        driver.setToken(token);
        // 已登录司机集合（接单用）
        DriverReceiving driverReceiving = DtoUtil.TbDriverToDriverReceiving(tbDriver);
        driverReceiving.setDriverLongitude(driverLongitude);     // 司机位置经度
        driverReceiving.setDriverLatitude(driverLatitude);       // 司机位置纬度
        driverReceiving.setToken(token);
        System.out.println(driver+ " "+driverReceiving);
        // 司机信息写入redis, key: "DRIVER_LOGIN:token" value: "driver"
        jedisClient.set(RedisKeyPrefixConstant.DRIVER_LOGIN_KEY_PRE + ":" + token, new Gson().toJson(driver));
        jedisClient.expire(RedisKeyPrefixConstant.DRIVER_LOGIN_KEY_PRE + ":" + token, SESSION_EXPIRE);
        // 司机接单信息写入redis, key: "DRIVER_LISTEN_ORDER_PRE" value: "driverReceivingList"
        jedisClient.hset(RedisKeyConstant.DRIVER_LISTEN_ORDER_PRE, String.valueOf(driver.getId()), new Gson().toJson(driverReceiving));
        return driver;
    }

    @Override
    public Driver getDriverToken(String token) {
        String json = jedisClient.get("SESSION:" + token);
        if (json == null) {
            Driver driver = new Driver();
            driver.setUseStatus(0);
            driver.setMessage("用户登录已过期");
            return driver;
        }
        // 重置过期时间
        jedisClient.expire(RedisKeyPrefixConstant.DRIVER_LOGIN_KEY_PRE + ":" + token, SESSION_EXPIRE);
        Driver driver = new Gson().fromJson(json, Driver.class);
        return driver;
    }

    @Override
    public int logout(String token) {
        return 0;
    }
}
